package com.zhang.teachingcommunity_spring.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zhang.teachingcommunity_spring.controller.resultType.ScorePage;
import com.zhang.teachingcommunity_spring.controller.resultType.UserPage;
import com.zhang.teachingcommunity_spring.mapper.CourseMapper;
import com.zhang.teachingcommunity_spring.mapper.SchoolMapper;
import com.zhang.teachingcommunity_spring.mapper.ScoreMapper;
import com.zhang.teachingcommunity_spring.mapper.entity.Course;
import com.zhang.teachingcommunity_spring.mapper.entity.School;
import com.zhang.teachingcommunity_spring.mapper.entity.Score;
import com.zhang.teachingcommunity_spring.mapper.entity.User;
import com.zhang.teachingcommunity_spring.service.ScoreService;
import com.zhang.teachingcommunity_spring.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/score")
public class ScoreController {
    @Autowired
    private ScoreService scoreService;
    @Autowired
    private ScoreMapper scoreMapper;
    @Autowired
    private SchoolMapper schoolMapper;
    @Autowired
    private CourseMapper courseMapper;
    @PostMapping("/findAll")
    public Result findAll(@RequestBody ScorePage scorePage){
        IPage<Score> all = scoreService.findAll(scorePage.getScore(),scorePage.getPage());
        return Result.success("获取成功！", all);
    }
    @PostMapping("/add")
    public Result addScore(@RequestBody Score score){
        scoreMapper.insert(score);
        return Result.success("添加成功！", true);
    }
    @GetMapping("/delete")
    public Result delete( Integer id){
        scoreMapper.deleteById(id);
        return Result.success("删除成功！", true);
    }
    @PostMapping("/update")
    public Result update(@RequestBody Score score){
        scoreMapper.updateById(score);
        return Result.success("修改成功！", true);
    }
    @GetMapping("/getDifferentCourseCount")
    private Result getDifferentCourseCount( ) {
        List<Map<String, Object>> differentCourseCount = scoreMapper.getDifferentCourseCount();
        return Result.success("获取成功",differentCourseCount);
    }
    @GetMapping("/getScoreByStuNo")
    private Result getScoreByStuNo(String stuNo) {
        QueryWrapper<Score> scoreQueryWrapper = new QueryWrapper<>();
        scoreQueryWrapper.eq("stu_no", stuNo);
        List<Score> scores = scoreMapper.selectList(scoreQueryWrapper);
        scores.forEach(item -> {
            Integer schoolId = item.getSchoolId();
            Integer courseId = item.getCourseId();
            School school = schoolMapper.selectById(schoolId);
            Course course = courseMapper.selectById(courseId);
            item.setSchool(school);
            item.setCourses(course);
        });
        return Result.success("获取成功",scores);
    }
}
